<?php

namespace App\Admin\Forms;

use App\Models\Order\OrderSample;
use App\Models\User\FamilyUser;
use Dcat\Admin\Contracts\LazyRenderable;
use Dcat\Admin\Traits\LazyWidget;
use Dcat\Admin\Widgets\Form;
use Illuminate\Support\Facades\Log;

class OrderSampleChecker extends Form implements LazyRenderable
{
    use LazyWidget;

    public function handle(array $input)
    {
        $id = $this->payload['id'] ?? null;
        $orderSample = OrderSample::find($id);
        $orderSample->checker_id = $input['checker_id'] ?? 0;
        $orderSample->name = $input['name'] ?? '';
        $orderSample->gender = $input['gender'] ?? 0;
        $orderSample->age = $input['age'] ?? 0;
        $orderSample->relationship_type = $input['relationship_type'] ?? 0;
        $orderSample->contact = $input['contact'] ?? '';
        $orderSample->id_card = $input['id_card'] ?? '';
        $orderSample->save();
        return $this
            ->response()
            ->success('保存成功')
            ->refresh();
    }

    public function form()
    {
        $id = $this->payload['id'] ?? null;
        $orderSample = OrderSample::find($id);
        $this->select('checker_id', '绑定人家庭成员')->options(FamilyUser::where('user_id', $orderSample->binder_id)->pluck('name', 'id'))->value($orderSample->checker_id);
        $this->text('name', '姓名')->value($orderSample->name);
        $this->radio('gender', '性别')->options([1 => '男', 2 => '女'])->value($orderSample->gender);
        $this->text('age', '年龄')->value($orderSample->age);
        $this->select('relationship_type', '关系类型')->options(trans('family-user.options.identity'))->value($orderSample->relationship_type);
        $this->text('contact', '联系电话')->value($orderSample->contact);
        $this->text('id_card', '身份证')->value($orderSample->id_card);
    }
}
